﻿using AnesSystem.Controls;
using AnesSystem.Models;
using AnesSystem.Models.Model;
using AnesSystem.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace AnesSystem.ViewModels
{
    public class LaborStatusItemViewModel : ViewModel
    {
        #region Variables
        private ObservableCollection<FrameworkElement> _statusList;
        private List<PatientLaborEventModel> _eventList;
        private List<PatientLaborEventModel> _beforeList;
        private List<PatientLaborEventModel> _afterList;
        private int index;


        #endregion


        #region Constructor
        public LaborStatusItemViewModel()
        {
            StatusList = new ObservableCollection<FrameworkElement>();
            EventList = new List<PatientLaborEventModel>();
            BeforeList = new List<PatientLaborEventModel>();
            AfterList = new List<PatientLaborEventModel>();
            InitData();
        }




        #endregion


        #region Properties

        public ObservableCollection<FrameworkElement> StatusList
        {
            get
            {
                return _statusList;
            }

            set
            {
                _statusList = value;
            }
        }

        public List<PatientLaborEventModel> EventList
        {
            get
            {
                return _eventList;
            }

            set
            {
                _eventList = value;
            }
        }

        public List<PatientLaborEventModel> BeforeList
        {
            get
            {
                return _beforeList;
            }

            set
            {
                _beforeList = value;
            }
        }

        public List<PatientLaborEventModel> AfterList
        {
            get
            {
                return _afterList;
            }

            set
            {
                _afterList = value;
            }
        }
        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods
        private void InitData()
        {
            DateTime startTime = PatientLink.Status.LaborRecordStartTime;
            DateTime oneHourEndTime = startTime.AddHours(1);
            EventList = PatientLaborEventService.Instance.GetPatientEvents8Time(PatientLink.Id.ToString(), PatientLink.Status.InLaborTime);
            EventList.Sort((PatientLaborEventModel o1, PatientLaborEventModel o2) =>
            {
                if (o1.BeginTime == o2.BeginTime)
                {
                    if (o1.InputTime < o2.InputTime)
                    {
                        return -1;
                    }
                    else
                    {
                        return 1;
                    }
                }
                else if (o1.BeginTime < o2.BeginTime)
                {
                    return -1;
                }
                else if (o1.BeginTime > o2.BeginTime)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            });
            foreach (var item in EventList)
            {
                if (item.BeginTime <= oneHourEndTime)
                {
                    BeforeList.Add(item);
                }
                else
                {
                    AfterList.Add(item);
                }
            }
            index = BeforeList.Count;
            CreateBeforeCtrl();
            CreateAfterCtrl();


        }

        private void CreateAfterCtrl()
        {

            for (int i = 0; i < AfterList.Count; i++)
            {
                AddOperEventMark((i + index + 1) + "", AfterList[i].BeginTime);
            }
        }

        private void CreateBeforeCtrl()
        {
            for (int i = 0; i < BeforeList.Count; i++)
            {
                int startNo = i;
                for (int j = (i + 1); j < BeforeList.Count; j++)
                {
                    if ((BeforeList[j].BeginTime - EventList[startNo].BeginTime).TotalMinutes > 5)
                    {
                        break;
                    }
                    i++;
                    continue;
                }
                if (i == startNo)
                {
                    AddOperEventMark(i + 1 + "", EventList[startNo].BeginTime);
                }
                if ((i - startNo) >= 2)
                {
                    AddOperEventMark((startNo + 1) + "-" + (i + 1), EventList[startNo].BeginTime);
                }
                else
                {
                    AddOperEventMark(startNo + 1 + "", EventList[startNo].BeginTime);
                    AddOperEventMark(i + 1 + "", EventList[i].BeginTime);
                }
            }
        }

        private void AddOperEventMark(string v, DateTime beginTime)
        {
            OperStatusTextCtrl ctrl = new OperStatusTextCtrl(v);
            ctrl.SetPoint(CalX(beginTime, ctrl), 10);
            StatusList.Add(ctrl);
        }

        private double CalX(DateTime beginTime, OperStatusTextCtrl ctrl)
        {
            try
            {
                DateTime startTime = PatientLink.Status.LaborRecordStartTime;
                DateTime oneHourEndTime = startTime.AddHours(1);
                List<PatientVitalSignsModel> timeList = PatientLink.LaborAnalgesia.LaborVitalData.TimeList;
                int minIndex = -1;
                double minMinutes;
                DateTime beforeTime = new DateTime();
                DateTime afterTime = new DateTime();
                if (beginTime <= oneHourEndTime)
                {
                    TimeSpan span = beginTime - startTime;
                    double span1 = span.Minutes + span.Hours * 60 + span.Days * 24 * 60;
                    double x = -1;
                    if (span1 != 0)
                    {
                        x = span1 / 5 * LocationUtil.RealTimeItem_Width - 3;
                    }
                    else
                    {
                        x = 0;
                    }

                    return Convert.ToInt32(x);
                }
                else
                {
                    minMinutes = (beginTime - timeList[0].CaptureTime).TotalMinutes;
                    for (int i = 0; i < timeList.Count; i++)
                    {
                        if (timeList[i].CaptureTime < beginTime)
                        {
                            double times = (beginTime - timeList[i].CaptureTime).TotalMinutes;

                            if (times < minMinutes)
                            {
                                minMinutes = times;
                                minIndex = i;
                            }
                        }
                    }
                    beforeTime = timeList[minIndex].CaptureTime;
                    if (timeList.Count - 1 > minIndex)
                    {
                        afterTime = timeList[minIndex + 1].CaptureTime;
                    }

                    if (afterTime.Equals(new DateTime()))
                    {
                        return 49 * LocationUtil.RealTimeItem_Width;
                    }
                    else if ((minIndex != -1) && !beforeTime.Equals(new DateTime()))
                    {
                        double timeSpan = (afterTime - beforeTime).TotalMinutes;
                        double min = (beginTime - beforeTime).TotalMinutes;
                        double x1 = ((minIndex - 12) + minIndex) * LocationUtil.RealTimeItem_Width;
                        double x2 = LocationUtil.RealTimeItem_Width * 2 * min / timeSpan;
                        return (int)(x1 + x2 - 1);
                    }
                    else
                    {
                        return 0;
                    }

                }
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
                return 0;
            }
        }

        #endregion


        #region Public/ProtectedMethods
        public void Refresh()
        {
            StatusList.Clear();
            EventList.Clear();
            BeforeList.Clear();
            AfterList.Clear();
            InitData();
        }

        #endregion


        #region EventHandlers


        #endregion
    }
}
